/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rhythm.commons.validation;

import com.rhythm.base.*;
import com.rhythm.commons.collections.Arrays;
import com.rhythm.commons.collections.Lists;


/**
 *
 * @author mlee
 */
public class InvalidStateException extends Exception {

    private final static long serialVersionUID = 1082009L;
    private final Messages messages;

    public InvalidStateException(final Messages messages){
        super(createExceptionMessage(messages));
        this.messages = messages;

    }

    /**
     * Creates a readable exception message for the {@code Exception} super class
     * @param messages
     * @return
     */
    private static String createExceptionMessage(final Messages messages) {

        StringBuilder message = new StringBuilder();

        for (int i = 0; i < messages.size(); i++) {
            message.append(i + 1).append(".) ").append(messages.get(i).getMessage());
            if (Lists.isLastItem(i, messages)) {
                message.append(Strings.LINE_SEPARATOR);
            }
        }
        return message.toString();
    }
}
